package com.slf.engine.services.impl;

import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.springframework.web.context.WebApplicationContext;

import com.slf.engine.base.IBaseDao;
import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContext;
import com.slf.engine.services.IReloadService;
import com.slf.engine.services.helper.ServiceHelper;

public class ReloadServiceImpl implements IReloadService {

	private static final Logger log = Logger.getLogger(ReloadServiceImpl.class);
	private WebApplicationContext context;
	public ReloadServiceImpl(WebApplicationContext context)
	{
		log.log(Priority.INFO, "===重载服务启动===");
		this.context = context;
	}
	/**
	 * 重载未发送订单
	 */
	public boolean reloadUnSendOrder()
	{
		IBaseDao baseDao = (IBaseDao)context.getBean("baseDao");
		try {
			
			List<Order> list = baseDao.getList("reloadUnSendOrders");
			log.log(Priority.INFO, "已加载未发送订单[" + list.size() + "]");
			SysContext.ORDER_LIST.addAll(list);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR, "数据库操作失败");
			return false;
		} catch (Exception e) {
			// TODO: handle exception
			log.log(Priority.ERROR, "数据库连接失败--3秒钟后重试");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
			}
			return reloadUnNoticeOrder();
		}
	}
	
	/**
	 * 重载未通知订单
	 */
	public boolean reloadUnNoticeOrder()
	{
		IBaseDao baseDao = (IBaseDao)context.getBean("baseDao");
		try {
			List<Order> list = baseDao.getList("reloadNoticeOrders");
			log.log(Priority.INFO, "已加载未通知订单[" + list.size() + "]");
			SysContext.NOTICE_LIST.addAll(list);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR, "数据库操作失败");
			return false;
		} catch (Exception e) {
			
			// TODO: handle exception
			log.log(Priority.ERROR, "数据库连接失败--3秒钟后重试");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
			}
			return reloadUnNoticeOrder();
		}
	}
	
	/**
	 * 重载未查询订单
	 */
	public boolean reloadUnQueryOrder()
	{
		IBaseDao baseDao = (IBaseDao)context.getBean("baseDao");
		try {
			List<Order> list = baseDao.getList("reloadUnBackOrders");
			log.log(Priority.INFO, "已加载未查询订单[" + list.size() + "]");
			for(int i = 0; i < list.size(); i++)
			{
				Order order = list.get(i);
				ServiceHelper.putToCache(order, context);
			}
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR, "数据库操作失败");
			return false;
		} catch (Exception e) {
			
			// TODO: handle exception
			log.log(Priority.ERROR, "数据库连接失败--3秒钟后重试");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
			}
			return reloadUnNoticeOrder();
		}
	}
}
